// 初始化应用
document.addEventListener('DOMContentLoaded', function() {
    // 初始化认证管理器
    window.authManager = new AuthManager();
    
    // 检查用户是否已登录
    if (window.authManager.checkAuth()) {
        // 如果用户已登录且当前在登录/注册页面，重定向到仪表盘
        if (window.location.pathname.includes('login.html') || 
            window.location.pathname.includes('register.html') ||
            window.location.pathname.includes('index.html')) {
            window.location.href = 'dashboard.html';
        }
    } else {
        // 如果用户未登录且当前在需要认证的页面，重定向到登录页
        if (window.location.pathname.includes('dashboard.html')) {
            window.location.href = 'login.html';
        }
    }
    
    // 设置导航事件监听器
    setupNavigation();
});

// 设置导航
function setupNavigation() {
    // 登录/注册按钮事件
    const loginBtn = document.getElementById('loginBtn');
    const registerBtn = document.getElementById('registerBtn');
    const logoutBtn = document.getElementById('logoutBtn');
    
    if (loginBtn) {
        loginBtn.addEventListener('click', function() {
            window.location.href = 'login.html';
        });
    }
    
    if (registerBtn) {
        registerBtn.addEventListener('click', function() {
            window.location.href = 'register.html';
        });
    }
    
    if (logoutBtn) {
        logoutBtn.addEventListener('click', function() {
            window.authManager.logout();
            window.location.href = 'index.html';
        });
    }
}